package com.food.user.controller;


import com.food.user.commos.Result;
import com.food.user.dto.UserAccountInfoDto;
import com.food.user.exception.MyParameterException;
import com.food.user.form.UpdateUserAccountInfoForm;
import com.food.user.service.UserAccountInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月17日
 */
@RestController
@RequestMapping("/userAccountInfo")
@Api(tags = "用户账号详细信息接口")
public class UserAccountInfoController {
    @Autowired
    private UserAccountInfoService userAccountInfoService;


    @GetMapping("/showUserAccountInfo")
    @ApiOperation("查询个人详细信息")
    public Result showUserAccountInfo(){
        UserAccountInfoDto userAccountInfoDto =userAccountInfoService.showUserAccountInfo();
        return Result.success(200,"查询个人账号详细信息成功",userAccountInfoDto);
    }


    @PostMapping("/updateUserAccountInfo")
    @ApiOperation("更新账号详细信息")
    private Result updateUserAccountInfo(@RequestBody @Valid UpdateUserAccountInfoForm form, BindingResult br){
        if (br.hasErrors()) {
            List<ObjectError> allErrors = br.getAllErrors();
            for (ObjectError allError : allErrors) {
                //获取打印提示信息
                System.out.println(allError.getDefaultMessage());
            }
            throw new MyParameterException(500, "参数校验失败");
        }
        userAccountInfoService.updateUserAccountInfo(form);
        return Result.success(200,"更新完善个人信息成功",null);
    }

}

